package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.Type;
import com.ruoyi.system.service.ITypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 组卷类别Controller
 * 
 * @author tq
 * @date 2025-04-19
 */
@RestController
@RequestMapping("/system/type")
public class TypeController extends BaseController
{
    @Autowired
    private ITypeService typeService;

    /**
     * 查询组卷类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public AjaxResult list(Type type)
    {
        List<Type> list = typeService.selectTypeList(type);
        return success(list);
    }

    /**
     * 查询组卷类别列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list/exclude/{typeId}")
    public AjaxResult excludeChild(@PathVariable(value = "typeId", required = false) Long typeId)
    {
        List<Type> types = typeService.selectTypeList(new Type());
        types.removeIf(d -> d.getTypeId().intValue() == typeId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), typeId + ""));
        return success(types);
    }




    /**
     * 导出组卷类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "组卷类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Type type)
    {
        List<Type> list = typeService.selectTypeList(type);
        ExcelUtil<Type> util = new ExcelUtil<Type>(Type.class);
        util.exportExcel(response, list, "组卷类别数据");
    }

    /**
     * 获取组卷类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return success(typeService.selectTypeByTypeId(typeId));
    }

    /**
     * 新增组卷类别
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "组卷类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Type type)
    {
        if (!typeService.checkTypeNameUnique(type))
        {
            return error("新增组卷分类'" + type.getName() + "'失败，组卷分类名称已存在");
        }
        type.setCreateBy(getUsername());
        return toAjax(typeService.insertType(type));
    }

    /**
     * 修改组卷类别
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "组卷类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Type type)
    {
        Long typeId = type.getTypeId();
        if (!typeService.checkTypeNameUnique(type))
        {
            return error("修改组卷类别'" + type.getName() + "'失败，组卷类别名称已存在");
        }
        else if (type.getParentId().equals(typeId))
        {
            return error("修改组卷类别'" + type.getName() + "'失败，上级类别不能是自己");
        }
        type.setUpdateBy(getUsername());
        return toAjax(typeService.updateType(type));
    }

    /**
     * 删除组卷类别
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "组卷类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long[] typeIds)
    {
        return typeService.deleteTypeByTypeIds(typeIds);
    }
}
